package com.devgroupvn.dota.youtube;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

public class Util
{


	public static String formatDurationFromMs(long duration, boolean padding)
	{
		duration = duration / 1000;
		long hh = duration / 3600;
		long mm = (duration % 3600) / 60;
		long ss = duration % 60;
		String text = "";
		if (padding)
		{
			if (hh > 0)
			{
				text = String.format("%02d:%02d:%02d", hh, mm, ss);
			}
			else
			{
				text = String.format("%02d:%02d", mm, ss);
			}
		}
		else
		{
			if (hh > 0)
			{
				text = String.format("%d:%d:%02d", hh, mm, ss);
			}
			else
			{
				text = String.format("%d:%02d", mm, ss);
			}
		}
		return text;
	}

	public static JSONArray getJsonYoutube(List<YoutubeData> _list)
	{
		JSONArray result = new JSONArray();
		for (int i = 0; i < _list.size(); i++)
		{
			YoutubeData entry = _list.get(i);
			if (entry._isSelected)
			{
				JSONObject object = new JSONObject();
				try
				{
					object.put("author", entry.getUploaded());
					object.put("duration", entry.getDuration());
					object.put("title", entry.getVideoName());
					object.put("url_id", entry.getMediaId());
					object.put("thumb_link", entry.getThumbnailLink());
					object.put("video_link", entry.getVideoLink());
					object.put("rating", entry._rating);
					object.put("format", entry.getFormat());
					object.put("view_count", entry.getViewCount());
					object.put("direct_link", entry._directlink);
					result.put(object);

				}
				catch (JSONException e)
				{
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	public static List<YoutubeData> getYoutubeItem(String strJson) throws JSONException
	{
		List<YoutubeData> _ArrayList = new ArrayList<YoutubeData>();
		
		JSONArray jsonArray  =new JSONObject(strJson).getJSONObject("data").getJSONArray("items");
		for(int i =0; i<jsonArray.length() ;i++)
		{
			YoutubeData entry = new YoutubeData();
			JSONObject objEntry = jsonArray.getJSONObject(i);
			entry.setId(objEntry.getString("id"));
			entry.setMediaId(objEntry.getString("id"));
			entry.setUploaded(objEntry.getString("uploader"));
			entry.setVideoName(objEntry.getString("title"));
			entry.setDesc(objEntry.getString("description"));
			entry.setDuration(objEntry.getString("duration"));
			entry.setViewCount(objEntry.getString("viewCount"));
			entry.setThumbnailLink(objEntry.getJSONObject("thumbnail").getString("hqDefault"));
			entry.setVideoLink(objEntry.getJSONObject("player").getString("default"));
			entry.setFormat("");
			_ArrayList.add(entry);
		}
		return _ArrayList;
		
	}
	
	public static String hashString(String string)
	{
		String hashword = null;
		try
		{
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(string.getBytes());
			BigInteger hash = new BigInteger(1, md5.digest());
			hashword = hash.toString(16);
		}
		catch (NoSuchAlgorithmException nsae)
		{
		}
		return hashword;
	}

	
	static Paint SHADOW_PAINT;
	final static float SHADOW_RADIUS = 8.5f;
	final static int SHADOW_COLOR = 0xEE000000;
	final static double SHADOW_RATIO = 0.07;
	public static Bitmap createShadow(Bitmap source, float roudedRadius)
	{
		SHADOW_PAINT = new Paint();
		SHADOW_PAINT.setShadowLayer(SHADOW_RADIUS / 2, 0.0f, 0.0f, SHADOW_COLOR);
		SHADOW_PAINT.setAntiAlias(true);
		SHADOW_PAINT.setFilterBitmap(true);
		SHADOW_PAINT.setColor(0xFF000000);
		SHADOW_PAINT.setStyle(Paint.Style.FILL);

		if (source == null)
			return null;

		final float bitmapWidth = (float) source.getWidth();
		final float bitmapHeight = (float) source.getHeight();

		final Canvas canvas = new Canvas();

		int extraW = (int) (source.getWidth() * SHADOW_RATIO);
		int extraH = (int) (source.getHeight() * SHADOW_RATIO);

		Bitmap bitmap = Bitmap.createBitmap(source.getWidth() + extraW, source.getHeight() + extraH, Bitmap.Config.ARGB_8888);

		canvas.setBitmap(bitmap);

		final RectF to = new RectF(0, 0, bitmapWidth, bitmapHeight);
		// int a = (int)SHADOW_RADIUS / 2;
		// final RectF shadow = new RectF(a, a,
		// bitmapWidth + a,
		// bitmapHeight+ a);
		final RectF to2 = new RectF(extraW, extraH, bitmapWidth, bitmapHeight);
		if (roudedRadius != 0)
			canvas.drawRoundRect(to2, roudedRadius, roudedRadius, SHADOW_PAINT);
		else
			canvas.drawRect(to2, SHADOW_PAINT);

		canvas.drawBitmap(source, null, to, null);
		// source.recycle();
		return bitmap;
	}

}
